import { http } from '../request/index';

export namespace AuthApi {
  /** 登录接口参数 */
  export interface LoginParams {
    password: string;
    username: string;
  }

  /** 登录接口返回值 */
  export interface LoginResult {
    accessToken: string;
    desc: string;
    realName: string;
    userId: string;
    username: string;
  }

  export interface RefreshTokenResult {
    data: string;
    status: number;
  }
}

/**
 * 登录
 */
export async function loginApi(data: AuthApi.LoginParams) {
  return http.post('/uc/uaa/validateAccount', {
    data,
    config: {
      transform(rawData) {
        if (rawData.access_token) {
          rawData.accessToken = rawData.access_token;
          rawData.realName = rawData.display_name;
          return rawData;
        }
        return rawData;
      },
    },
  });
}

/**
 * 刷新accessToken
 */
export async function refreshTokenApi() {
  return http.post('/auth/refresh');
}

/**
 * 退出登录
 */
export async function logoutApi() {
  return http.post('/uc/uaa/logout');
}

/**
 * 获取用户权限码
 */
export async function getAccessCodesApi() {
  return http.post('/auth/codes');
}

/**
 * 获取用户信息
 */
export async function getUserInfoApi() {
  return http.get('/system/auth/getUserInfo', {});
}
